package com.hushan.elevator.dao;

import com.hushan.elevator.model.DeviceLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface DeviceLogRepository extends JpaRepository<DeviceLog, Integer> {

    @Query(value = """
            select \
            devl.log_id,
            devl.business_type,
            devl.log_message,
            devl.event_id,
            devl.detail,
            devl.created_time,
            devl.device_serial,
            devi.device_id,
            devi.device_name \
            from \
            device_log as devl INNER JOIN \
            device_info AS devi inner join \
            auth_dept as d \
            ON devl.device_serial = devi.serial_number AND devi.dept_id = d.dept_id \
            WHERE \
            (:deptId IS NULL OR (FIND_IN_SET(:deptId, d.ancestors) OR d.dept_id = :deptId)) \
            and (:deviceSerial IS NULL OR devi.serial_number = :deviceSerial) \
            and d.is_deleted=0 \
            and devi.is_deleted=0""",
            nativeQuery = true)
    Page<Object[]> findDeviceLog(
            @Param("deptId") Integer deptId,
            @Param("deviceSerial") String deviceSerial,
            Pageable pageable
    );
}